//
//  QZHomeFollowUserCollectionCell.m
//  shortvideoplay
//
//  Created by tikeios_002 on 2024/7/22.
//

#import "QZHomeFollowUserCollectionCell.h"
#import "SYCollectionViewRoundFlowLayout.h"
#import "QZHomeFollowUserCell.h"
#import "QZHomeFollowUserHeaderView.h"
#import "QZOtherMyVC.h"

@interface QZHomeFollowUserCollectionCell ()
@property (assign, nonatomic)CGSize itemSize;
@end
@implementation QZHomeFollowUserCollectionCell

- (void)showOldAddNavViewToMainOldView1{
    UIView * oldBgSS = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
    oldBgSS.clipsToBounds = YES;
    [self addSubview:oldBgSS];
    
    UIButton * sendBtn = [[UIButton alloc]initWithFrame:CGRectMake(KScreen_W-75, kStatusBarHeight+10, 60, 46)];
    sendBtn.layer.cornerRadius = 3;
    sendBtn.backgroundColor = [UIColor whiteColor];
    [sendBtn setTitle:@"发送" forState:UIControlStateNormal];
    [sendBtn setTitleColor:ColorHex(@"#333333") forState:UIControlStateNormal];
    sendBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    [sendBtn addTarget:self action:@selector(sendTap) forControlEvents:UIControlEventTouchUpInside];
    [oldBgSS addSubview:sendBtn];
    
    UIView * sendBg = [[UIView alloc]initWithFrame:CGRectMake(15, kStatusBarHeight+66, KScreen_W-30, (KScreen_H-KSafeAreaBottomHeight-25-(kStatusBarHeight+66))/2)];
    sendBg.backgroundColor = [UIColor whiteColor];
    sendBg.layer.cornerRadius = 3;
    [oldBgSS addSubview:sendBg];
    
    UILabel * sendTip = [[UILabel alloc]initWithFrame:CGRectMake(0, (sendBg.frame.size.height-40)/2, sendBg.frame.size.width, 40)];
    sendTip.text = @"发送区";
    sendTip.textColor = ColorHex(@"#f0f0f0");
    sendTip.textAlignment = NSTextAlignmentCenter;
    sendTip.font = [UIFont systemFontOfSize:28];
    [sendBg addSubview:sendTip];
}
- (void)awakeFromNib {
    [super awakeFromNib];
    CGFloat w = (KScreen_W-3)/2;
    _itemSize = CGSizeMake(w, (w/180)*240);
    
    SYCollectionViewRoundFlowLayout *layout = [[SYCollectionViewRoundFlowLayout alloc]init];
    layout.sectionHeadersPinToVisibleBounds = YES;
    layout.collectionCellAlignmentType = SYCollectionViewFlowLayoutAlignmentTypeByLeft;
    _collection.collectionViewLayout = layout;
    [_collection registerNib:[UINib nibWithNibName:@"QZHomeFollowUserCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"QZHomeFollowUserCell"];
    [_collection registerNib:[UINib nibWithNibName:@"QZHomeFollowUserHeaderView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader  withReuseIdentifier:@"QZHomeFollowUserHeaderView"];
    _collection.backgroundColor = viewback_black;
    self.collection.delegate = self;
    self.collection.dataSource = self;
}
-(void)configUser:(NSArray *)userList{
    self.userList = userList;
    [_collection reloadData];
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.userList.count;
}
-(__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    QZUserInfoModel * model = self.userList[indexPath.row];
    QZHomeFollowUserCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"QZHomeFollowUserCell" forIndexPath:indexPath];
    [cell configUserModel:model];
    KWeakSelf
    cell.QZUserFollowHomeBlock = ^(NSInteger tag) {
        if (tag == 0) {
            [weakSelf followsUser:indexPath.row];
        }else{
            [QZHongBaoView share].hidden = YES;
            [[QZHongBaoView share] stopClick:NO];
            QZUserInfoModel * model = weakSelf.userList[indexPath.row];
            QZOtherMyVC * other = [[QZOtherMyVC alloc]init];
            other.otherMid = model.mid;
            [[Tools currentActiveController].navigationController pushViewController:other animated:YES];
        }
        
    };
    return cell;
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return _itemSize;
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    return CGSizeMake(KScreen_W, 55+KNav_H);
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    if (kind == UICollectionElementKindSectionHeader) {
        QZHomeFollowUserHeaderView * header = (QZHomeFollowUserHeaderView *)[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"QZHomeFollowUserHeaderView" forIndexPath:indexPath];
        header.backgroundColor = viewback_black;
        return header;
    }else{
        UICollectionReusableView * header = nil;
        return header;
    }
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    QZUserInfoModel * model = _userList[indexPath.row];
    QZOtherMyVC * other = [[QZOtherMyVC alloc]init];
    other.otherMid = model.mid;
    [[Tools currentActiveController].navigationController pushViewController:other animated:YES];
}
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, 0, 0, 0);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(nonnull UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 2;
}
-(CGFloat)collectionView:(UICollectionView*)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section

{
    return 1;

}

- (void)followsUser:(NSInteger)row{
    QZUserInfoModel * model = _userList[row];
    KWeakSelf
    KMutableDic(sendDic)
    [sendDic setObject:model.mid forKey:@"to_mid"];
    [YBNetBase userFollowUserRequest:sendDic target:nil successBlock:^(NSDictionary * _Nonnull data, NSInteger code, NSString * _Nonnull msg) {
        if(code == 200){
            if([model.isfollow intValue] == 1){
                model.isfollow = @(0);
                model.total_fans = @([model.total_fans intValue]-1);
                [YBMsgPop showPop:@"已取消关注"];
            }else{
                model.isfollow = @(1);
                model.total_fans = @([model.total_fans intValue]+1);
                [YBMsgPop showPop:NSLocalizedString(@"已关注", nil)];
            }
            [weakSelf.collection reloadData];
        }else{
            [YBMsgPop showPop:msg];
        }
    }];
}

@end
